package com.cn.common.implement;

import cn.com.allunion.sms.pojo.Telaccsegment;
import cn.com.allunion.sms.service.mybatis.TelaccsegmentService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class CheckTypeImpl {
	public static final Logger logger = LoggerFactory.getLogger(CheckTypeImpl.class);
	/**
	 * 手机号码集合
	 */
	public List<String> mobileNumbers;
	/**
	 * 中国联通号码集合
	 */
	public List<String> unicomNumbers;
	/**
	 * 中国电信号码集合
	 */
	public List<String> telecomNumbers;
	/**
	 * 卫星通信号码集合
	 */
	public List<String> satcomNubmers;

//	TelaccsegmentAction ta;
	TelaccsegmentService telaccsegmentService;

	/**
	 * 根据电话号码检测运营商
	 * 
	 * @param phone 电话号码
	 * @return 运营商代码
	 */
	public int checkthePhone(String phone) {
        if (logger.isDebugEnabled()) {
            logger.debug("---> 进入checkTypeImpl");
        }
		int resultCode = 11;
		// 去掉前缀取得手机号码
		if (phone.startsWith("0") || phone.startsWith("+860")) {
			phone = phone.substring(phone.indexOf("1"), phone.length());
		}
		// mobile参数为空或者手机号码长度不为11，错误！
		if (phone == null || phone.trim().length() != 11) {
			return -1;
		}

        List<Telaccsegment> telaccsegmentList = telaccsegmentService.findTelaccsegment(phone) ;

        if (CollectionUtils.isNotEmpty(telaccsegmentList)) {
            if (logger.isDebugEnabled()) {
                logger.debug("telaccsegmentService.findTelaccsegment({}): {}", phone, telaccsegmentList.get(0).getIspId());
            }
            switch (telaccsegmentList.get(0).getIspId()) {
                case 0:
                    // 检测是否为联通号段
                    resultCode = 10010;
                    break;
                case 1:
                    // 检测是否为移动号段
                    resultCode = 10086;
                    break;
                case 2:
                    // 检测是否为电信号段
                    resultCode = 10000;
                    break;
                // case 3:
                // // 检测是否为卫通号段
                // resultCode = 11000;
                // break;
                default:
                    // 未知运营商
                    resultCode = 0;
                    break;
            }
        } else { // 在数据库中没有找到对应的运营商
            resultCode = 0 ;
        }
		return resultCode;
	}

	/**
	 * 手机号码集合
	 */
	public List<String> getMobileNumbers() {
		return mobileNumbers;
	}

	/**
	 * 手机号码集合
	 */
	public void setMobileNumbers(List<String> mobileNumbers) {
		this.mobileNumbers = mobileNumbers;
	}

	/**
	 * 中国联通号码集合
	 */
	public List<String> getUnicomNumbers() {
		return unicomNumbers;
	}

	/**
	 * 中国联通号码集合
	 */
	public void setUnicomNumbers(List<String> unicomNumbers) {
		this.unicomNumbers = unicomNumbers;
	}

	/**
	 * 中国电信号码集合
	 */
	public List<String> getTelecomNumbers() {
		return telecomNumbers;
	}

	/**
	 * 中国电信号码集合
	 */
	public void setTelecomNumbers(List<String> telecomNumbers) {
		this.telecomNumbers = telecomNumbers;
	}

	/**
	 * 卫星通信号码集合
	 */
	public List<String> getSatcomNubmers() {
		return satcomNubmers;
	}

	/**
	 * 卫星通信号码集合
	 */
	public void setSatcomNubmers(List<String> satcomNubmers) {
		this.satcomNubmers = satcomNubmers;
	}

	public TelaccsegmentService getTelaccsegmentService() {
		return telaccsegmentService;
	}

	public void setTelaccsegmentService(TelaccsegmentService ta) {
		this.telaccsegmentService = ta;
	}

}
